<?php
require_once('lanceur_spip.php');
include_spip('functions/pdk_common');
require_once(_DIR_PLUGIN_PDK.'classes/simpletest/class.PdkModelTest.php');

class TestClassProductModel extends PdkModelTest {
    /**
     *  CurrencyModel
     */
    protected $currency_mdl;
    /**
     *  RubriqueCurrencyModel
     */
    private $rub_cur_mdl;
    
    /**
     *  PdkRubriqueModel
     */
    private $rub_mdl;
    
    /**
     *  PdkArticleModel
     */
    private $art_mdl;
    
    public function __construct(){
        parent::__construct();
        
        // Define all additional models:
        $pdk_models = array();
        $this->rub_cur_mdl = pdk_load_class('RubriqueCurrencyModel','classes');
        $pdk_models['RubriqueCurrencyModel'] = $this->rub_cur_mdl;
        $this->rub_mdl = pdk_load_class('PdkRubriqueModel','classes/models');
        $pdk_models['PdkRubriqueModel'] = $this->rub_mdl;        
        $this->art_mdl = pdk_load_class('PdkArticleModel','classes/models');
        $pdk_models['PdkArticleModel'] = $this->art_mdl;
        $this->currency_mdl = pdk_load_class('CurrencyModel','classes');
        $pdk_models['CurrencyModel'] = $this->currency_mdl;
        
        $this->AddPdkModels($pdk_models);
        
        // Set main (tested) model:
        $this->SetPdkModel('ProductModel', pdk_load_class('ProductModel', 'classes'));
    }
    
    /**
     *  Run before each test
     */
    function setUp() {
        parent::setUp();    
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        parent::tearDown();
    }
    
    function __destruct() {
        // Call parent destructor to delete created tables
        parent::__destruct();
    }
    
    function testGetProductPrice() {
        // Price, converted to currency wich is attached to article 1
        $price = $this->model->GetProductPrice(1);        
        $price_check = $this->model->test_data[0]['price']*$this->currency_mdl->test_data[0]['rate'];        
        $this->assertEqual($price_check, $price);
        
        // Price, directly converted to some currency
        $price = $this->model->GetProductPrice(1, $this->currency_mdl->test_data[1]['code']);
        $price_check = $this->model->test_data[0]['price']*$this->currency_mdl->test_data[1]['rate'];
        $this->assertEqual($price_check, $price);
        
        // old Price, converted to currency wich is attached to article 1
        $price = $this->model->GetProductPrice(1, false, 'priceold');        
        $price_check = $this->model->test_data[0]['priceold']*$this->currency_mdl->test_data[0]['rate'];        
        $this->assertEqual($price_check, $price);
        
        // Old Price, directly converted to some currency
        $price = $this->model->GetProductPrice(1, $this->currency_mdl->test_data[1]['code'], 'priceold');
        $price_check = $this->model->test_data[0]['priceold']*$this->currency_mdl->test_data[1]['rate'];
        $this->assertEqual($price_check, $price);
    }
    
    function testGetMaxIdEquipment() {        
        $this->assertEqual(2, $this->model->GetMaxIdEquipment());
        $this->model->insert(array('id_equipment'=>4));
        $this->assertEqual(4, $this->model->GetMaxIdEquipment());
    }
    
    function testInsertOneRow_DataWithoutIdEquipment() {
        $data = $this->model->test_data[0];
        $data[$this->model->primary_key] = 999;
        unset($data['id_equipment']);
        
        $data_check = $data;
        $data_check['id_equipment'] = $this->model->GetMaxIdEquipment()+1;
        
        $this->model->insert($data);
        
        $data_sel = $this->model->select_by_id($data[$this->model->primary_key]);
        //var_dump($this->model);
        $this->assertEqual($data_check, $data_sel);
    }
}
